﻿using SubtitleDownloader.Providers;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Windows.Forms;

namespace SubtitleDownloader
{
	[Serializable]
	public class Data
	{
		public List<string> FileExtensions { get; set; }
		public int MaxSimConnections { get; set; }
		public int UpdateInterval { get; set; }
		public Dictionary<string, string> CustomNameTranslator { get; set; }
		public List<string> FileNameFilters { get; set; }
		//public List<string> WatchedFolders { get; set; }
		public List<string> WatchedSeriesFolders { get; set; }
		public List<string> WatchedMoviesFolders { get; set; }
		public List<ISubtitleProvider> SubtitlesProviders
		{
			get
			{
				return new List<ISubtitleProvider>() {
					new Subscenter(), new Subtitles()
				};
			}
		}

		#region Singleton / Save / Load

		private static string _savePath = Directory.GetCurrentDirectory() + "\\Data.sd";

		private static Data _instance;
		public static Data Instance
		{
			get
			{
				if (_instance == null)
					_instance = Load();
				return _instance;
			}
		}

		private Data()
		{
			CheckData();
		}

		private void CheckData()
		{
			if (WatchedSeriesFolders == null)
				WatchedSeriesFolders = new List<string>();
			if (WatchedMoviesFolders == null)
				WatchedMoviesFolders = new List<string>();
			if (FileNameFilters == null)
				FileNameFilters = new List<string>();
			if (MaxSimConnections == 0)
				MaxSimConnections = 8;
			if (UpdateInterval == 0)
				UpdateInterval = 15;
			if (CustomNameTranslator == null)
				CustomNameTranslator = new Dictionary<string, string>();
			if (FileExtensions == null)
				FileExtensions = new List<string>() {
					".avi", ".mkv", ".mp4", ".mov"
				};
		}

		public static void Save()
		{
			try {
				if (File.Exists(_savePath))
					File.Delete(_savePath);
			} catch (Exception e) {
				String.Format("Cannot delete {0}, {1}", _savePath, e.Message).AsErrorMessage("Serialization error");
				return;
			}

			try {
				using (var file = File.OpenWrite(_savePath)) {
					var serializer = new BinaryFormatter();
					serializer.Serialize(file, _instance);
				}
			} catch (Exception e) {
				String.Format("Cannot write to {0}, {1}", _savePath, e.Message).AsErrorMessage("Serialization error");
			}
		}

		private static Data Load()
		{
			if (!File.Exists(_savePath))
				return new Data();

			object des = null;
			try {
				using (var file = File.OpenRead(_savePath)) {
					var serializer = new BinaryFormatter();
					des = serializer.Deserialize(file);
				}
			} catch (Exception e) {
				String.Format("Cannot read {0}, {1}", _savePath, e.Message).AsErrorMessage("Deserialization error");
			}

			if (des == null ? true : !(des is Data)) {
				try { File.Delete(_savePath); } catch { }
				return new Data();
			}

			var ret = des as Data;
			if (ret != null)
				ret.CheckData();
			return ret;
		}

		#endregion
	}
}
